package cn.dujc.superkid.activities;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.Fragement_testAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.WrongQuestionResult;
import cn.dujc.superkid.fragment.ErrorFragment;
import cn.dujc.superkid.utils.Preferences;

public class ErrorQueActivity extends BaseActivity {
    private static final String TAG = "";
    private TabLayout tabLayout = null;
    private ViewPager vp_pager;
    private TextView allError,allYes,weekError,weekYes,todayError,todayYes;
    @Override
    public int getViewId() {
        return R.layout.activity_error_que;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void initBasic(Bundle savedInstanceState) {

        ImageLoader imageLoader = ImageLoader.getInstance();//ImageLoader需要实例化
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        tabLayout = findViewById(R.id.error_tablayout);
        vp_pager = findViewById(R.id.error_viewpager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(vp_pager);
        allError=findViewById(R.id.error_all_error_num);
        allYes=findViewById(R.id.error_all_yes_num);
        weekError=findViewById(R.id.error_week_error_num);
        weekYes=findViewById(R.id.error_week_yes_num);
        todayYes=findViewById(R.id.error_today_yes_num);
        todayError=findViewById(R.id.error_today_error_num);
        setTitle("错题本");
        loadData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initLogic(WrongQuestionResult data) {
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < data.getCategory().size(); i++) {
            String name=data.getCategory().get(i).getName();
            titles.add(name);
            tabLayout.addTab(tabLayout.newTab().setText(name));
        }

        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < data.getCategory().size(); i++) {
            ErrorFragment fragment =new ErrorFragment();
            Bundle bd=new Bundle();
            bd.putCharSequence("category_id",i+1+"");
            fragment.setArguments(bd);
            fragments.add(fragment);
        }
        //适配器
        Fragement_testAdapter fragementAdapter = new Fragement_testAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置adapter
        vp_pager.setAdapter(fragementAdapter);
        //将Tablayout和ViewPager关联起来
        tabLayout.setupWithViewPager(vp_pager);
        //给TaBlayout设置适配器
        tabLayout.setTabsFromPagerAdapter(fragementAdapter);
    }

    private void initViews(WrongQuestionResult.PageInfoBean data) {
//        private TextView allError,allYes,weekError,weekYes,todayError,todayYes;
          allError.setText(data.getAll_num());
          allYes.setText(data.getAll_correction_num());
          weekError.setText(data.getWeek_num());
          weekYes.setText(data.getWeek_correction_num());
          todayError.setText(data.getToday_remaining_num());
          todayYes.setText(data.getToday_correction_num());
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberWrongQuestion(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .create()).enqueue(new ResultCallBack<Result<WrongQuestionResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<WrongQuestionResult> body) {
                final WrongQuestionResult.PageInfoBean data =body.getData().getPageInfo();
                initViews(data);
                initLogic(body.getData());
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
            }
        });
    }
}
